﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Text.RegularExpressions;

namespace KaoQin.WPFApp.ValidationHelper
{
    public class UserNameValidationRule : ValidationRule
    {
        public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
        {
            if (string.IsNullOrEmpty(value.ToString()))
            {
                return new ValidationResult(false, "不能为空");
            }
            if (!Regex.IsMatch(value.ToString(), "^[0-9a-zA-Z_]{1,20}$"))
            {
                return new ValidationResult(false, "只能是数字、字母或下划线");
            }
            if (value.ToString().Length > 20)
            {
                return new ValidationResult(false, "长度只能是1到20位");
            }

            return ValidationResult.ValidResult;
        }
    }
}
